unit Common.Config;

interface

uses
    System.SysUtils, System.Classes, System.IniFiles;

type
    TAppConfig = class(TPersistent)
    private
        iniFile: TIniFile;
        function GetAppPath: string;
        function GetDefault: TAppConfig;
    public
        constructor Create();
        destructor Destroy; override;
        function ReadString(Section: string; Key: string; default: string): string;
        function ReadInt(Section: string; Key: string; Default: Integer): integer;
        function ReadInt64(Section: string; key: string; Default: Int64): Int64;
        function ReadBool(Section: string; Key: string; Default: Boolean): Boolean;
        function ReadFloat(Section: string; Key: string; Default: Double): Double;
        procedure WriteString(Section: string; Key: string; value: string);
        procedure WriteInt(Section: string; Key: string; value: Integer); overload;
        procedure WriteInt(Section: string; Key: string; value: Int64); overload;
        procedure WriteBool(Section: string; Key: string; value: Boolean);
        procedure WriteFloat(Section: string; Key: string; value: Double);
        function ExistKey(Section: string; Key: string): Boolean;
        function GetAllSections(list: TStrings): Boolean;
        procedure DeleteSection(Section: string);
        procedure DeleteKey(Section: string; key: string);
        property AppPath: string read GetAppPath;
        property Default: TAppConfig read GetDefault;
    end;

var
    GAppConfig: TAppConfig;

implementation

var
    varAppPath: string;

{ TAppConfig }

constructor TAppConfig.Create;
begin
    inherited create();
    inifile := TIniFile.Create(AppPath + 'config.ini');
end;

procedure TAppConfig.DeleteKey(Section, key: string);
begin
    iniFile.DeleteKey(Section, key);
end;

procedure TAppConfig.DeleteSection(Section: string);
var
    keys: TStringList;
    i: integer;
begin
    keys := TStringList.Create;
    iniFile.ReadSection(Section, keys);
    for i := 0 to keys.Count - 1 do
    begin
        iniFile.DeleteKey(Section, keys[i]);
    end;
end;

destructor TAppConfig.Destroy;
begin
    iniFile.Free;
    inherited;
end;

function TAppConfig.ExistKey(Section, Key: string): Boolean;
begin
    result := iniFile.ValueExists(Section, Key);
end;

function TAppConfig.GetAllSections(list: TStrings): Boolean;
begin
    try
        iniFile.ReadSections(list);
        result := true;
    except
        on E: Exception do
        begin
            result := false;
        end;
    end;
end;

function TAppConfig.GetAppPath: string;
begin
    result := varAppPath;
end;

function TAppConfig.GetDefault: TAppConfig;
begin
    result := GAppConfig;
end;

function TAppConfig.ReadBool(Section, Key: string; Default: Boolean): Boolean;
begin
    result := iniFile.ReadBool(Section, Key, Default);
end;

function TAppConfig.ReadFloat(Section, Key: string; Default: Double): Double;
begin
    result := iniFile.ReadFloat(Section, Key, Default);
end;

function TAppConfig.ReadInt(Section, Key: string; Default: Integer): integer;
begin
    result := iniFile.ReadInteger(Section, Key, Default);
end;

function TAppConfig.ReadInt64(Section, key: string; Default: Int64): Int64;
begin
    result := inifile.ReadInt64(Section, key, Default);
end;

function TAppConfig.ReadString(Section, Key, default: string): string;
begin
    result := iniFile.ReadString(Section, Key, default);
    if not iniFile.ValueExists(Section, Key) then
    begin
        iniFile.WriteString(Section, Key, default);
    end;
end;

procedure TAppConfig.WriteBool(Section, Key: string; value: Boolean);
begin
    iniFile.WriteBool(Section, Key, value);
end;

procedure TAppConfig.WriteFloat(Section, Key: string; value: Double);
begin
    inifile.WriteFloat(Section, Key, value);
end;

procedure TAppConfig.WriteInt(Section, Key: string; value: Int64);
begin
    iniFile.WriteInt64(Section, Key, value);
end;

procedure TAppConfig.WriteInt(Section, Key: string; value: Integer);
begin
    iniFile.WriteInteger(Section, Key, value);
end;

procedure TAppConfig.WriteString(Section, Key, value: string);
begin
    iniFile.WriteString(Section, Key, value);
end;

initialization
    varAppPath := ExtractFilePath(ParamStr(0));
    GAppConfig := TAppConfig.Create;


finalization
    FreeAndNil(GAppConfig);

end.

